<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/solver.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">src/caffe</a> - solver.cpp<span style="font-size: 80%;"> (source / <a href="solver.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">209</td>
            <td class="headerCovTableEntry">276</td>
            <td class="headerCovTableEntryMed">75.7 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">17</td>
            <td class="headerCovTableEntry">40</td>
            <td class="headerCovTableEntryLo">42.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;cstdio&gt;</a>
<span class="lineNum">       2 </span>            : 
<span class="lineNum">       3 </span>            : #include &lt;string&gt;
<span class="lineNum">       4 </span>            : #include &lt;vector&gt;
<span class="lineNum">       5 </span>            : 
<span class="lineNum">       6 </span>            : #include &quot;boost/algorithm/string.hpp&quot;
<span class="lineNum">       7 </span>            : #include &quot;caffe/solver.hpp&quot;
<span class="lineNum">       8 </span>            : #include &quot;caffe/util/format.hpp&quot;
<span class="lineNum">       9 </span>            : #include &quot;caffe/util/hdf5.hpp&quot;
<span class="lineNum">      10 </span>            : #include &quot;caffe/util/io.hpp&quot;
<span class="lineNum">      11 </span>            : #include &quot;caffe/util/upgrade_proto.hpp&quot;
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : namespace caffe {
<a name="14"><span class="lineNum">      14 </span>            : </a>
<span class="lineNum">      15 </span>            : template&lt;typename Dtype&gt;
<span class="lineNum">      16 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::SetActionFunction(ActionCallback func) {</span>
<span class="lineNum">      17 </span><span class="lineCov">          1 :   action_request_function_ = func;</span>
<span class="lineNum">      18 </span><span class="lineCov">          1 : }</span>
<a name="19"><span class="lineNum">      19 </span>            : </a>
<span class="lineNum">      20 </span>            : template&lt;typename Dtype&gt;
<span class="lineNum">      21 </span><span class="lineCov">      12100 : SolverAction::Enum Solver&lt;Dtype&gt;::GetRequestedAction() {</span>
<span class="lineNum">      22 </span><span class="lineCov">      12100 :   if (action_request_function_) {</span>
<span class="lineNum">      23 </span>            :     // If the external request function has been set, call it.
<span class="lineNum">      24 </span><span class="lineCov">      12100 :     return action_request_function_();</span>
<span class="lineNum">      25 </span>            :   }
<span class="lineNum">      26 </span>            :   return SolverAction::NONE;
<span class="lineNum">      27 </span>            : }
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      30 </span><span class="lineCov">          1 : Solver&lt;Dtype&gt;::Solver(const SolverParameter&amp; param)</span>
<span class="lineNum">      31 </span><span class="lineCov">          2 :     : net_(), callbacks_(), requested_early_exit_(false) {</span>
<span class="lineNum">      32 </span><span class="lineCov">          1 :   Init(param);</span>
<span class="lineNum">      33 </span><span class="lineCov">          1 : }</span>
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      36 </span><span class="lineNoCov">          0 : Solver&lt;Dtype&gt;::Solver(const string&amp; param_file)</span>
<span class="lineNum">      37 </span><span class="lineNoCov">          0 :     : net_(), callbacks_(), requested_early_exit_(false) {</span>
<span class="lineNum">      38 </span><span class="lineNoCov">          0 :   SolverParameter param;</span>
<span class="lineNum">      39 </span><span class="lineNoCov">          0 :   ReadSolverParamsFromTextFileOrDie(param_file, &amp;param);</span>
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :   Init(param);</span>
<span class="lineNum">      41 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      42 </span>            : 
<span class="lineNum">      43 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      44 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::Init(const SolverParameter&amp; param) {</span>
<span class="lineNum">      45 </span><span class="lineCov">          3 :   LOG_IF(INFO, Caffe::root_solver()) &lt;&lt; &quot;Initializing solver from parameters: &quot;</span>
<span class="lineNum">      46 </span><span class="lineCov">          3 :     &lt;&lt; std::endl &lt;&lt; param.DebugString();</span>
<span class="lineNum">      47 </span><span class="lineCov">          1 :   param_ = param;</span>
<span class="lineNum">      48 </span><span class="lineCov">          1 :   CHECK_GE(param_.average_loss(), 1) &lt;&lt; &quot;average_loss should be non-negative.&quot;;</span>
<span class="lineNum">      49 </span><span class="lineCov">          1 :   CheckSnapshotWritePermissions();</span>
<span class="lineNum">      50 </span><span class="lineCov">          1 :   if (param_.random_seed() &gt;= 0) {</span>
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :     Caffe::set_random_seed(param_.random_seed() + Caffe::solver_rank());</span>
<span class="lineNum">      52 </span>            :   }
<span class="lineNum">      53 </span>            :   // Scaffolding code
<span class="lineNum">      54 </span><span class="lineCov">          1 :   InitTrainNet();</span>
<span class="lineNum">      55 </span><span class="lineCov">          1 :   InitTestNets();</span>
<span class="lineNum">      56 </span><span class="lineCov">          1 :   if (Caffe::root_solver()) {</span>
<span class="lineNum">      57 </span><span class="lineCov">          1 :     LOG(INFO) &lt;&lt; &quot;Solver scaffolding done.&quot;;</span>
<span class="lineNum">      58 </span>            :   }
<span class="lineNum">      59 </span><span class="lineCov">          1 :   iter_ = 0;</span>
<span class="lineNum">      60 </span><span class="lineCov">          1 :   current_step_ = 0;</span>
<span class="lineNum">      61 </span><span class="lineCov">          1 : }</span>
<span class="lineNum">      62 </span>            : 
<span class="lineNum">      63 </span>            : // Load weights from the caffemodel(s) specified in &quot;weights&quot; solver parameter
<a name="64"><span class="lineNum">      64 </span>            : // into the train and test nets.</a>
<span class="lineNum">      65 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      66 </span><span class="lineNoCov">          0 : void LoadNetWeights(shared_ptr&lt;Net&lt;Dtype&gt; &gt; net,</span>
<span class="lineNum">      67 </span>            :     const std::string&amp; model_list) {
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :   std::vector&lt;std::string&gt; model_names;</span>
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :   boost::split(model_names, model_list, boost::is_any_of(&quot;,&quot;));</span>
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; model_names.size(); ++i) {</span>
<span class="lineNum">      71 </span><span class="lineNoCov">          0 :     boost::trim(model_names[i]);</span>
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Finetuning from &quot; &lt;&lt; model_names[i];</span>
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :     net-&gt;CopyTrainedLayersFrom(model_names[i]);</span>
<span class="lineNum">      74 </span>            :   }
<span class="lineNum">      75 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      76 </span>            : 
<span class="lineNum">      77 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      78 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::InitTrainNet() {</span>
<span class="lineNum">      79 </span>            :   const int num_train_nets = param_.has_net() + param_.has_net_param() +
<span class="lineNum">      80 </span><span class="lineCov">          4 :       param_.has_train_net() + param_.has_train_net_param();</span>
<span class="lineNum">      81 </span><span class="lineCov">          1 :   const string field_names = &quot;net, net_param, train_net, train_net_param&quot;;</span>
<span class="lineNum">      82 </span><span class="lineCov">          1 :   CHECK_GE(num_train_nets, 1) &lt;&lt; &quot;SolverParameter must specify a train net &quot;</span>
<span class="lineNum">      83 </span>            :       &lt;&lt; &quot;using one of these fields: &quot; &lt;&lt; field_names;
<span class="lineNum">      84 </span><span class="lineCov">          1 :   CHECK_LE(num_train_nets, 1) &lt;&lt; &quot;SolverParameter must not contain more than &quot;</span>
<span class="lineNum">      85 </span>            :       &lt;&lt; &quot;one of these fields specifying a train_net: &quot; &lt;&lt; field_names;
<span class="lineNum">      86 </span><span class="lineCov">          2 :   NetParameter net_param;</span>
<span class="lineNum">      87 </span><span class="lineCov">          1 :   if (param_.has_train_net_param()) {</span>
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :     LOG_IF(INFO, Caffe::root_solver())</span>
<span class="lineNum">      89 </span>            :         &lt;&lt; &quot;Creating training net specified in train_net_param.&quot;;
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :     net_param.CopyFrom(param_.train_net_param());</span>
<span class="lineNum">      91 </span><span class="lineCov">          1 :   } else if (param_.has_train_net()) {</span>
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :     LOG_IF(INFO, Caffe::root_solver())</span>
<span class="lineNum">      93 </span>            :         &lt;&lt; &quot;Creating training net from train_net file: &quot; &lt;&lt; param_.train_net();
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :     ReadNetParamsFromTextFileOrDie(param_.train_net(), &amp;net_param);</span>
<span class="lineNum">      95 </span>            :   }
<span class="lineNum">      96 </span><span class="lineCov">          1 :   if (param_.has_net_param()) {</span>
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :     LOG_IF(INFO, Caffe::root_solver())</span>
<span class="lineNum">      98 </span>            :         &lt;&lt; &quot;Creating training net specified in net_param.&quot;;
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :     net_param.CopyFrom(param_.net_param());</span>
<span class="lineNum">     100 </span>            :   }
<span class="lineNum">     101 </span><span class="lineCov">          1 :   if (param_.has_net()) {</span>
<span class="lineNum">     102 </span><span class="lineCov">          2 :     LOG_IF(INFO, Caffe::root_solver())</span>
<span class="lineNum">     103 </span>            :         &lt;&lt; &quot;Creating training net from net file: &quot; &lt;&lt; param_.net();
<span class="lineNum">     104 </span><span class="lineCov">          1 :     ReadNetParamsFromTextFileOrDie(param_.net(), &amp;net_param);</span>
<span class="lineNum">     105 </span>            :   }
<span class="lineNum">     106 </span>            :   // Set the correct NetState.  We start with the solver defaults (lowest
<span class="lineNum">     107 </span>            :   // precedence); then, merge in any NetState specified by the net_param itself;
<span class="lineNum">     108 </span>            :   // finally, merge in any NetState specified by the train_state (highest
<span class="lineNum">     109 </span>            :   // precedence).
<span class="lineNum">     110 </span><span class="lineCov">          2 :   NetState net_state;</span>
<span class="lineNum">     111 </span>            :   net_state.set_phase(TRAIN);
<span class="lineNum">     112 </span><span class="lineCov">          1 :   net_state.MergeFrom(net_param.state());</span>
<span class="lineNum">     113 </span><span class="lineCov">          1 :   net_state.MergeFrom(param_.train_state());</span>
<span class="lineNum">     114 </span><span class="lineCov">          1 :   net_param.mutable_state()-&gt;CopyFrom(net_state);</span>
<span class="lineNum">     115 </span><span class="lineCov">          1 :   net_.reset(new Net&lt;Dtype&gt;(net_param));</span>
<span class="lineNum">     116 </span><span class="lineCov">          1 :   for (int w_idx = 0; w_idx &lt; param_.weights_size(); ++w_idx) {</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :     LoadNetWeights(net_, param_.weights(w_idx));</span>
<span class="lineNum">     118 </span>            :   }
<span class="lineNum">     119 </span><span class="lineCov">          1 : }</span>
<span class="lineNum">     120 </span>            : 
<span class="lineNum">     121 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     122 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::InitTestNets() {</span>
<span class="lineNum">     123 </span>            :   const bool has_net_param = param_.has_net_param();
<span class="lineNum">     124 </span>            :   const bool has_net_file = param_.has_net();
<span class="lineNum">     125 </span><span class="lineCov">          1 :   const int num_generic_nets = has_net_param + has_net_file;</span>
<span class="lineNum">     126 </span><span class="lineCov">          1 :   CHECK_LE(num_generic_nets, 1)</span>
<span class="lineNum">     127 </span>            :       &lt;&lt; &quot;Both net_param and net_file may not be specified.&quot;;
<span class="lineNum">     128 </span>            :   const int num_test_net_params = param_.test_net_param_size();
<span class="lineNum">     129 </span>            :   const int num_test_net_files = param_.test_net_size();
<span class="lineNum">     130 </span><span class="lineCov">          1 :   const int num_test_nets = num_test_net_params + num_test_net_files;</span>
<span class="lineNum">     131 </span><span class="lineCov">          1 :   if (num_generic_nets) {</span>
<span class="lineNum">     132 </span><span class="lineCov">          1 :       CHECK_GE(param_.test_iter_size(), num_test_nets)</span>
<span class="lineNum">     133 </span>            :           &lt;&lt; &quot;test_iter must be specified for each test network.&quot;;
<span class="lineNum">     134 </span>            :   } else {
<span class="lineNum">     135 </span><span class="lineNoCov">          0 :       CHECK_EQ(param_.test_iter_size(), num_test_nets)</span>
<span class="lineNum">     136 </span>            :           &lt;&lt; &quot;test_iter must be specified for each test network.&quot;;
<span class="lineNum">     137 </span>            :   }
<span class="lineNum">     138 </span>            :   // If we have a generic net (specified by net or net_param, rather than
<span class="lineNum">     139 </span>            :   // test_net or test_net_param), we may have an unlimited number of actual
<span class="lineNum">     140 </span>            :   // test networks -- the actual number is given by the number of remaining
<span class="lineNum">     141 </span>            :   // test_iters after any test nets specified by test_net_param and/or test_net
<span class="lineNum">     142 </span>            :   // are evaluated.
<span class="lineNum">     143 </span>            :   const int num_generic_net_instances = param_.test_iter_size() - num_test_nets;
<span class="lineNum">     144 </span>            :   const int num_test_net_instances = num_test_nets + num_generic_net_instances;
<span class="lineNum">     145 </span><span class="lineCov">          1 :   if (param_.test_state_size()) {</span>
<span class="lineNum">     146 </span><span class="lineNoCov">          0 :     CHECK_EQ(param_.test_state_size(), num_test_net_instances)</span>
<span class="lineNum">     147 </span>            :         &lt;&lt; &quot;test_state must be unspecified or specified once per test net.&quot;;
<span class="lineNum">     148 </span>            :   }
<span class="lineNum">     149 </span><span class="lineCov">          1 :   if (num_test_net_instances) {</span>
<span class="lineNum">     150 </span><span class="lineCov">          1 :     CHECK_GT(param_.test_interval(), 0);</span>
<span class="lineNum">     151 </span>            :   }
<span class="lineNum">     152 </span>            :   int test_net_id = 0;
<span class="lineNum">     153 </span><span class="lineCov">          3 :   vector&lt;string&gt; sources(num_test_net_instances);</span>
<span class="lineNum">     154 </span><span class="lineCov">          2 :   vector&lt;NetParameter&gt; net_params(num_test_net_instances);</span>
<span class="lineNum">     155 </span><span class="lineCov">          1 :   for (int i = 0; i &lt; num_test_net_params; ++i, ++test_net_id) {</span>
<span class="lineNum">     156 </span><span class="lineNoCov">          0 :       sources[test_net_id] = &quot;test_net_param&quot;;</span>
<span class="lineNum">     157 </span><span class="lineNoCov">          0 :       net_params[test_net_id].CopyFrom(param_.test_net_param(i));</span>
<span class="lineNum">     158 </span>            :   }
<span class="lineNum">     159 </span><span class="lineCov">          1 :   for (int i = 0; i &lt; num_test_net_files; ++i, ++test_net_id) {</span>
<span class="lineNum">     160 </span><span class="lineNoCov">          0 :       sources[test_net_id] = &quot;test_net file: &quot; + param_.test_net(i);</span>
<span class="lineNum">     161 </span><span class="lineNoCov">          0 :       ReadNetParamsFromTextFileOrDie(param_.test_net(i),</span>
<span class="lineNum">     162 </span>            :           &amp;net_params[test_net_id]);
<span class="lineNum">     163 </span>            :   }
<span class="lineNum">     164 </span><span class="lineCov">          1 :   const int remaining_test_nets = param_.test_iter_size() - test_net_id;</span>
<span class="lineNum">     165 </span><span class="lineCov">          1 :   if (has_net_param) {</span>
<span class="lineNum">     166 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; remaining_test_nets; ++i, ++test_net_id) {</span>
<span class="lineNum">     167 </span><span class="lineNoCov">          0 :       sources[test_net_id] = &quot;net_param&quot;;</span>
<span class="lineNum">     168 </span><span class="lineNoCov">          0 :       net_params[test_net_id].CopyFrom(param_.net_param());</span>
<span class="lineNum">     169 </span>            :     }
<span class="lineNum">     170 </span>            :   }
<span class="lineNum">     171 </span><span class="lineCov">          1 :   if (has_net_file) {</span>
<span class="lineNum">     172 </span><span class="lineCov">          3 :     for (int i = 0; i &lt; remaining_test_nets; ++i, ++test_net_id) {</span>
<span class="lineNum">     173 </span><span class="lineCov">          2 :       sources[test_net_id] = &quot;net file: &quot; + param_.net();</span>
<span class="lineNum">     174 </span><span class="lineCov">          1 :       ReadNetParamsFromTextFileOrDie(param_.net(), &amp;net_params[test_net_id]);</span>
<span class="lineNum">     175 </span>            :     }
<span class="lineNum">     176 </span>            :   }
<span class="lineNum">     177 </span><span class="lineCov">          2 :   test_nets_.resize(num_test_net_instances);</span>
<span class="lineNum">     178 </span><span class="lineCov">          3 :   for (int i = 0; i &lt; num_test_net_instances; ++i) {</span>
<span class="lineNum">     179 </span>            :     // Set the correct NetState.  We start with the solver defaults (lowest
<span class="lineNum">     180 </span>            :     // precedence); then, merge in any NetState specified by the net_param
<span class="lineNum">     181 </span>            :     // itself; finally, merge in any NetState specified by the test_state
<span class="lineNum">     182 </span>            :     // (highest precedence).
<span class="lineNum">     183 </span><span class="lineCov">          2 :     NetState net_state;</span>
<span class="lineNum">     184 </span>            :     net_state.set_phase(TEST);
<span class="lineNum">     185 </span><span class="lineCov">          2 :     net_state.MergeFrom(net_params[i].state());</span>
<span class="lineNum">     186 </span><span class="lineCov">          1 :     if (param_.test_state_size()) {</span>
<span class="lineNum">     187 </span><span class="lineNoCov">          0 :       net_state.MergeFrom(param_.test_state(i));</span>
<span class="lineNum">     188 </span>            :     }
<span class="lineNum">     189 </span><span class="lineCov">          1 :     net_params[i].mutable_state()-&gt;CopyFrom(net_state);</span>
<span class="lineNum">     190 </span><span class="lineCov">          2 :     LOG(INFO)</span>
<span class="lineNum">     191 </span><span class="lineCov">          1 :         &lt;&lt; &quot;Creating test net (#&quot; &lt;&lt; i &lt;&lt; &quot;) specified by &quot; &lt;&lt; sources[i];</span>
<span class="lineNum">     192 </span><span class="lineCov">          2 :     test_nets_[i].reset(new Net&lt;Dtype&gt;(net_params[i]));</span>
<span class="lineNum">     193 </span>            :     test_nets_[i]-&gt;set_debug_info(param_.debug_info());
<span class="lineNum">     194 </span><span class="lineCov">          1 :     for (int w_idx = 0; w_idx &lt; param_.weights_size(); ++w_idx) {</span>
<span class="lineNum">     195 </span><span class="lineNoCov">          0 :       LoadNetWeights(test_nets_[i], param_.weights(w_idx));</span>
<span class="lineNum">     196 </span>            :     }
<span class="lineNum">     197 </span>            :   }
<span class="lineNum">     198 </span><span class="lineCov">          1 : }</span>
<span class="lineNum">     199 </span>            : 
<span class="lineNum">     200 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     201 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::Step(int iters) {</span>
<span class="lineNum">     202 </span><span class="lineCov">          1 :   const int start_iter = iter_;</span>
<span class="lineNum">     203 </span><span class="lineCov">          1 :   const int stop_iter = iter_ + iters;</span>
<span class="lineNum">     204 </span>            :   int average_loss = this-&gt;param_.average_loss();
<span class="lineNum">     205 </span>            :   losses_.clear();
<span class="lineNum">     206 </span><span class="lineCov">          1 :   smoothed_loss_ = 0;</span>
<span class="lineNum">     207 </span><span class="lineCov">          1 :   iteration_timer_.Start();</span>
<span class="lineNum">     208 </span>            : 
<span class="lineNum">     209 </span><span class="lineCov">      10001 :   while (iter_ &lt; stop_iter) {</span>
<span class="lineNum">     210 </span>            :     // zero-init the params
<span class="lineNum">     211 </span><span class="lineCov">      10000 :     net_-&gt;ClearParamDiffs();</span>
<span class="lineNum">     212 </span><span class="lineCov">      10000 :     if (param_.test_interval() &amp;&amp; iter_ % param_.test_interval() == 0</span>
<span class="lineNum">     213 </span>            :         &amp;&amp; (iter_ &gt; 0 || param_.test_initialization())) {
<span class="lineNum">     214 </span><span class="lineCov">         20 :       if (Caffe::root_solver()) {</span>
<span class="lineNum">     215 </span><span class="lineCov">         20 :         TestAll();</span>
<span class="lineNum">     216 </span>            :       }
<span class="lineNum">     217 </span><span class="lineCov">         20 :       if (requested_early_exit_) {</span>
<span class="lineNum">     218 </span>            :         // Break out of the while loop because stop was requested while testing.
<span class="lineNum">     219 </span>            :         break;
<span class="lineNum">     220 </span>            :       }
<span class="lineNum">     221 </span>            :     }
<span class="lineNum">     222 </span>            : 
<span class="lineNum">     223 </span><span class="lineCov">      20000 :     for (int i = 0; i &lt; callbacks_.size(); ++i) {</span>
<span class="lineNum">     224 </span><span class="lineNoCov">          0 :       callbacks_[i]-&gt;on_start();</span>
<span class="lineNum">     225 </span>            :     }
<span class="lineNum">     226 </span><span class="lineCov">      10000 :     const bool display = param_.display() &amp;&amp; iter_ % param_.display() == 0;</span>
<span class="lineNum">     227 </span><span class="lineCov">      10000 :     net_-&gt;set_debug_info(display &amp;&amp; param_.debug_info());</span>
<span class="lineNum">     228 </span>            :     // accumulate the loss and gradient
<span class="lineNum">     229 </span>            :     Dtype loss = 0;
<span class="lineNum">     230 </span><span class="lineCov">      30000 :     for (int i = 0; i &lt; param_.iter_size(); ++i) {</span>
<span class="lineNum">     231 </span><span class="lineCov">      10000 :       loss += net_-&gt;ForwardBackward();</span>
<span class="lineNum">     232 </span>            :     }
<span class="lineNum">     233 </span><span class="lineCov">      10000 :     loss /= param_.iter_size();</span>
<span class="lineNum">     234 </span>            :     // average the loss across iterations for smoothed reporting
<span class="lineNum">     235 </span><span class="lineCov">      10000 :     UpdateSmoothedLoss(loss, start_iter, average_loss);</span>
<span class="lineNum">     236 </span><span class="lineCov">      10000 :     if (display) {</span>
<span class="lineNum">     237 </span><span class="lineCov">        100 :       float lapse = iteration_timer_.Seconds();</span>
<span class="lineNum">     238 </span><span class="lineCov">        100 :       float per_s = (iter_ - iterations_last_) / (lapse ? lapse : 1);</span>
<span class="lineNum">     239 </span><span class="lineCov">        400 :       LOG_IF(INFO, Caffe::root_solver()) &lt;&lt; &quot;Iteration &quot; &lt;&lt; iter_</span>
<span class="lineNum">     240 </span>            :           &lt;&lt; &quot; (&quot; &lt;&lt; per_s &lt;&lt; &quot; iter/s, &quot; &lt;&lt; lapse &lt;&lt; &quot;s/&quot;
<span class="lineNum">     241 </span><span class="lineCov">        200 :           &lt;&lt; param_.display() &lt;&lt; &quot; iters), loss = &quot; &lt;&lt; smoothed_loss_;</span>
<span class="lineNum">     242 </span><span class="lineCov">        100 :       iteration_timer_.Start();</span>
<span class="lineNum">     243 </span><span class="lineCov">        100 :       iterations_last_ = iter_;</span>
<span class="lineNum">     244 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; result = net_-&gt;output_blobs();
<span class="lineNum">     245 </span>            :       int score_index = 0;
<span class="lineNum">     246 </span><span class="lineCov">        500 :       for (int j = 0; j &lt; result.size(); ++j) {</span>
<span class="lineNum">     247 </span><span class="lineCov">        100 :         const Dtype* result_vec = result[j]-&gt;cpu_data();</span>
<span class="lineNum">     248 </span>            :         const string&amp; output_name =
<span class="lineNum">     249 </span><span class="lineCov">        100 :             net_-&gt;blob_names()[net_-&gt;output_blob_indices()[j]];</span>
<span class="lineNum">     250 </span>            :         const Dtype loss_weight =
<span class="lineNum">     251 </span><span class="lineCov">        100 :             net_-&gt;blob_loss_weights()[net_-&gt;output_blob_indices()[j]];</span>
<span class="lineNum">     252 </span><span class="lineCov">        300 :         for (int k = 0; k &lt; result[j]-&gt;count(); ++k) {</span>
<span class="lineNum">     253 </span><span class="lineCov">        200 :           ostringstream loss_msg_stream;</span>
<span class="lineNum">     254 </span><span class="lineCov">        100 :           if (loss_weight) {</span>
<span class="lineNum">     255 </span><span class="lineCov">        100 :             loss_msg_stream &lt;&lt; &quot; (* &quot; &lt;&lt; loss_weight</span>
<span class="lineNum">     256 </span><span class="lineCov">        100 :                             &lt;&lt; &quot; = &quot; &lt;&lt; loss_weight * result_vec[k] &lt;&lt; &quot; loss)&quot;;</span>
<span class="lineNum">     257 </span>            :           }
<span class="lineNum">     258 </span><span class="lineCov">        300 :           LOG_IF(INFO, Caffe::root_solver()) &lt;&lt; &quot;    Train net output #&quot;</span>
<span class="lineNum">     259 </span><span class="lineCov">        200 :               &lt;&lt; score_index++ &lt;&lt; &quot;: &quot; &lt;&lt; output_name &lt;&lt; &quot; = &quot;</span>
<span class="lineNum">     260 </span><span class="lineCov">        300 :               &lt;&lt; result_vec[k] &lt;&lt; loss_msg_stream.str();</span>
<span class="lineNum">     261 </span>            :         }
<span class="lineNum">     262 </span>            :       }
<span class="lineNum">     263 </span>            :     }
<span class="lineNum">     264 </span><span class="lineCov">      20000 :     for (int i = 0; i &lt; callbacks_.size(); ++i) {</span>
<span class="lineNum">     265 </span><span class="lineNoCov">          0 :       callbacks_[i]-&gt;on_gradients_ready();</span>
<span class="lineNum">     266 </span>            :     }
<span class="lineNum">     267 </span><span class="lineCov">      10000 :     ApplyUpdate();</span>
<span class="lineNum">     268 </span>            : 
<span class="lineNum">     269 </span><span class="lineCov">      10000 :     SolverAction::Enum request = GetRequestedAction();</span>
<span class="lineNum">     270 </span>            : 
<span class="lineNum">     271 </span>            :     // Save a snapshot if needed.
<span class="lineNum">     272 </span><span class="lineCov">      20002 :     if ((param_.snapshot()</span>
<span class="lineNum">     273 </span><span class="lineCov">      10000 :          &amp;&amp; iter_ % param_.snapshot() == 0</span>
<span class="lineNum">     274 </span>            :          &amp;&amp; Caffe::root_solver()) ||
<span class="lineNum">     275 </span>            :          (request == SolverAction::SNAPSHOT)) {
<span class="lineNum">     276 </span><span class="lineCov">          2 :       Snapshot();</span>
<span class="lineNum">     277 </span>            :     }
<span class="lineNum">     278 </span><span class="lineCov">      10000 :     if (SolverAction::STOP == request) {</span>
<span class="lineNum">     279 </span><span class="lineNoCov">          0 :       requested_early_exit_ = true;</span>
<span class="lineNum">     280 </span>            :       // Break out of training loop.
<span class="lineNum">     281 </span><span class="lineNoCov">          0 :       break;</span>
<span class="lineNum">     282 </span>            :     }
<span class="lineNum">     283 </span>            :   }
<span class="lineNum">     284 </span><span class="lineCov">          1 : }</span>
<a name="285"><span class="lineNum">     285 </span>            : </a>
<span class="lineNum">     286 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     287 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::Solve(const char* resume_file) {</span>
<span class="lineNum">     288 </span><span class="lineCov">          2 :   CHECK(Caffe::root_solver());</span>
<span class="lineNum">     289 </span><span class="lineCov">          2 :   LOG(INFO) &lt;&lt; &quot;Solving &quot; &lt;&lt; net_-&gt;name();</span>
<span class="lineNum">     290 </span><span class="lineCov">          2 :   LOG(INFO) &lt;&lt; &quot;Learning Rate Policy: &quot; &lt;&lt; param_.lr_policy();</span>
<span class="lineNum">     291 </span>            : 
<span class="lineNum">     292 </span>            :   // Initialize to false every time we start solving.
<span class="lineNum">     293 </span><span class="lineCov">          1 :   requested_early_exit_ = false;</span>
<span class="lineNum">     294 </span>            : 
<span class="lineNum">     295 </span><span class="lineCov">          1 :   if (resume_file) {</span>
<span class="lineNum">     296 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Restoring previous solver status from &quot; &lt;&lt; resume_file;</span>
<span class="lineNum">     297 </span><span class="lineNoCov">          0 :     Restore(resume_file);</span>
<span class="lineNum">     298 </span>            :   }
<span class="lineNum">     299 </span>            : 
<span class="lineNum">     300 </span>            :   // For a network that is trained by the solver, no bottom or top vecs
<span class="lineNum">     301 </span>            :   // should be given, and we will just provide dummy vecs.
<span class="lineNum">     302 </span><span class="lineCov">          1 :   int start_iter = iter_;</span>
<span class="lineNum">     303 </span><span class="lineCov">          1 :   Step(param_.max_iter() - iter_);</span>
<span class="lineNum">     304 </span>            :   // If we haven't already, save a snapshot after optimization, unless
<span class="lineNum">     305 </span>            :   // overridden by setting snapshot_after_train := false
<span class="lineNum">     306 </span><span class="lineCov">          2 :   if (param_.snapshot_after_train()</span>
<span class="lineNum">     307 </span><span class="lineCov">          1 :       &amp;&amp; (!param_.snapshot() || iter_ % param_.snapshot() != 0)) {</span>
<span class="lineNum">     308 </span><span class="lineNoCov">          0 :     Snapshot();</span>
<span class="lineNum">     309 </span>            :   }
<span class="lineNum">     310 </span><span class="lineCov">          1 :   if (requested_early_exit_) {</span>
<span class="lineNum">     311 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Optimization stopped early.&quot;;</span>
<span class="lineNum">     312 </span><span class="lineNoCov">          0 :     return;</span>
<span class="lineNum">     313 </span>            :   }
<span class="lineNum">     314 </span>            :   // After the optimization is done, run an additional train and test pass to
<span class="lineNum">     315 </span>            :   // display the train and test loss/outputs if appropriate (based on the
<span class="lineNum">     316 </span>            :   // display and test_interval settings, respectively).  Unlike in the rest of
<span class="lineNum">     317 </span>            :   // training, for the train net we only run a forward pass as we've already
<span class="lineNum">     318 </span>            :   // updated the parameters &quot;max_iter&quot; times -- this final pass is only done to
<span class="lineNum">     319 </span>            :   // display the loss, which is computed in the forward pass.
<span class="lineNum">     320 </span><span class="lineCov">          1 :   if (param_.display() &amp;&amp; iter_ % param_.display() == 0) {</span>
<span class="lineNum">     321 </span>            :     int average_loss = this-&gt;param_.average_loss();
<span class="lineNum">     322 </span>            :     Dtype loss;
<span class="lineNum">     323 </span><span class="lineCov">          1 :     net_-&gt;Forward(&amp;loss);</span>
<span class="lineNum">     324 </span>            : 
<span class="lineNum">     325 </span><span class="lineCov">          1 :     UpdateSmoothedLoss(loss, start_iter, average_loss);</span>
<span class="lineNum">     326 </span>            : 
<span class="lineNum">     327 </span><span class="lineCov">          3 :     LOG(INFO) &lt;&lt; &quot;Iteration &quot; &lt;&lt; iter_ &lt;&lt; &quot;, loss = &quot; &lt;&lt; smoothed_loss_;</span>
<span class="lineNum">     328 </span>            :   }
<span class="lineNum">     329 </span><span class="lineCov">          1 :   if (param_.test_interval() &amp;&amp; iter_ % param_.test_interval() == 0) {</span>
<span class="lineNum">     330 </span><span class="lineCov">          1 :     TestAll();</span>
<span class="lineNum">     331 </span>            :   }
<span class="lineNum">     332 </span><span class="lineCov">          1 :   LOG(INFO) &lt;&lt; &quot;Optimization Done.&quot;;</span>
<span class="lineNum">     333 </span>            : }
<a name="334"><span class="lineNum">     334 </span>            : </a>
<span class="lineNum">     335 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     336 </span><span class="lineCov">         21 : void Solver&lt;Dtype&gt;::TestAll() {</span>
<span class="lineNum">     337 </span><span class="lineCov">        126 :   for (int test_net_id = 0;</span>
<span class="lineNum">     338 </span><span class="lineCov">         21 :        test_net_id &lt; test_nets_.size() &amp;&amp; !requested_early_exit_;</span>
<span class="lineNum">     339 </span>            :        ++test_net_id) {
<span class="lineNum">     340 </span><span class="lineCov">         21 :     Test(test_net_id);</span>
<span class="lineNum">     341 </span>            :   }
<span class="lineNum">     342 </span><span class="lineCov">         21 : }</span>
<span class="lineNum">     343 </span>            : 
<span class="lineNum">     344 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     345 </span><span class="lineCov">         21 : void Solver&lt;Dtype&gt;::Test(const int test_net_id) {</span>
<span class="lineNum">     346 </span><span class="lineCov">         42 :   CHECK(Caffe::root_solver());</span>
<span class="lineNum">     347 </span><span class="lineCov">         63 :   LOG(INFO) &lt;&lt; &quot;Iteration &quot; &lt;&lt; iter_</span>
<span class="lineNum">     348 </span><span class="lineCov">         21 :             &lt;&lt; &quot;, Testing net (#&quot; &lt;&lt; test_net_id &lt;&lt; &quot;)&quot;;</span>
<span class="lineNum">     349 </span><span class="lineCov">         42 :   CHECK_NOTNULL(test_nets_[test_net_id].get())-&gt;</span>
<span class="lineNum">     350 </span>            :       ShareTrainedLayersWith(net_.get());
<span class="lineNum">     351 </span>            :   vector&lt;Dtype&gt; test_score;
<span class="lineNum">     352 </span>            :   vector&lt;int&gt; test_score_output_id;
<span class="lineNum">     353 </span>            :   const shared_ptr&lt;Net&lt;Dtype&gt; &gt;&amp; test_net = test_nets_[test_net_id];
<span class="lineNum">     354 </span>            :   Dtype loss = 0;
<span class="lineNum">     355 </span><span class="lineCov">       4221 :   for (int i = 0; i &lt; param_.test_iter(test_net_id); ++i) {</span>
<span class="lineNum">     356 </span><span class="lineCov">       2100 :     SolverAction::Enum request = GetRequestedAction();</span>
<span class="lineNum">     357 </span>            :     // Check to see if stoppage of testing/training has been requested.
<span class="lineNum">     358 </span><span class="lineCov">       2100 :     while (request != SolverAction::NONE) {</span>
<span class="lineNum">     359 </span><span class="lineNoCov">          0 :         if (SolverAction::SNAPSHOT == request) {</span>
<span class="lineNum">     360 </span><span class="lineNoCov">          0 :           Snapshot();</span>
<span class="lineNum">     361 </span><span class="lineNoCov">          0 :         } else if (SolverAction::STOP == request) {</span>
<span class="lineNum">     362 </span><span class="lineNoCov">          0 :           requested_early_exit_ = true;</span>
<span class="lineNum">     363 </span>            :         }
<span class="lineNum">     364 </span><span class="lineNoCov">          0 :         request = GetRequestedAction();</span>
<span class="lineNum">     365 </span>            :     }
<span class="lineNum">     366 </span><span class="lineCov">       2100 :     if (requested_early_exit_) {</span>
<span class="lineNum">     367 </span>            :       // break out of test loop.
<span class="lineNum">     368 </span>            :       break;
<span class="lineNum">     369 </span>            :     }
<span class="lineNum">     370 </span>            : 
<span class="lineNum">     371 </span>            :     Dtype iter_loss;
<span class="lineNum">     372 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; result =
<span class="lineNum">     373 </span><span class="lineCov">       2100 :         test_net-&gt;Forward(&amp;iter_loss);</span>
<span class="lineNum">     374 </span><span class="lineCov">       2100 :     if (param_.test_compute_loss()) {</span>
<span class="lineNum">     375 </span><span class="lineNoCov">          0 :       loss += iter_loss;</span>
<span class="lineNum">     376 </span>            :     }
<span class="lineNum">     377 </span><span class="lineCov">       2100 :     if (i == 0) {</span>
<span class="lineNum">     378 </span><span class="lineCov">        126 :       for (int j = 0; j &lt; result.size(); ++j) {</span>
<span class="lineNum">     379 </span><span class="lineCov">         42 :         const Dtype* result_vec = result[j]-&gt;cpu_data();</span>
<span class="lineNum">     380 </span><span class="lineCov">        210 :         for (int k = 0; k &lt; result[j]-&gt;count(); ++k) {</span>
<span class="lineNum">     381 </span><span class="lineCov">         42 :           test_score.push_back(result_vec[k]);</span>
<span class="lineNum">     382 </span><span class="lineCov">         42 :           test_score_output_id.push_back(j);</span>
<span class="lineNum">     383 </span>            :         }
<span class="lineNum">     384 </span>            :       }
<span class="lineNum">     385 </span>            :     } else {
<span class="lineNum">     386 </span>            :       int idx = 0;
<span class="lineNum">     387 </span><span class="lineCov">      16632 :       for (int j = 0; j &lt; result.size(); ++j) {</span>
<span class="lineNum">     388 </span><span class="lineCov">       4158 :         const Dtype* result_vec = result[j]-&gt;cpu_data();</span>
<span class="lineNum">     389 </span><span class="lineCov">      12474 :         for (int k = 0; k &lt; result[j]-&gt;count(); ++k) {</span>
<span class="lineNum">     390 </span><span class="lineCov">       8316 :           test_score[idx++] += result_vec[k];</span>
<span class="lineNum">     391 </span>            :         }
<span class="lineNum">     392 </span>            :       }
<span class="lineNum">     393 </span>            :     }
<span class="lineNum">     394 </span>            :   }
<span class="lineNum">     395 </span><span class="lineCov">         21 :   if (requested_early_exit_) {</span>
<span class="lineNum">     396 </span><span class="lineNoCov">          0 :     LOG(INFO)     &lt;&lt; &quot;Test interrupted.&quot;;</span>
<span class="lineNum">     397 </span>            :     return;
<span class="lineNum">     398 </span>            :   }
<span class="lineNum">     399 </span><span class="lineCov">         21 :   if (param_.test_compute_loss()) {</span>
<span class="lineNum">     400 </span><span class="lineNoCov">          0 :     loss /= param_.test_iter(test_net_id);</span>
<span class="lineNum">     401 </span><span class="lineNoCov">          0 :     LOG(INFO) &lt;&lt; &quot;Test loss: &quot; &lt;&lt; loss;</span>
<span class="lineNum">     402 </span>            :   }
<span class="lineNum">     403 </span><span class="lineCov">        168 :   for (int i = 0; i &lt; test_score.size(); ++i) {</span>
<span class="lineNum">     404 </span>            :     const int output_blob_index =
<span class="lineNum">     405 </span><span class="lineCov">         84 :         test_net-&gt;output_blob_indices()[test_score_output_id[i]];</span>
<span class="lineNum">     406 </span><span class="lineCov">         42 :     const string&amp; output_name = test_net-&gt;blob_names()[output_blob_index];</span>
<span class="lineNum">     407 </span><span class="lineCov">         42 :     const Dtype loss_weight = test_net-&gt;blob_loss_weights()[output_blob_index];</span>
<span class="lineNum">     408 </span><span class="lineCov">         84 :     ostringstream loss_msg_stream;</span>
<span class="lineNum">     409 </span><span class="lineCov">         84 :     const Dtype mean_score = test_score[i] / param_.test_iter(test_net_id);</span>
<span class="lineNum">     410 </span><span class="lineCov">         42 :     if (loss_weight) {</span>
<span class="lineNum">     411 </span><span class="lineCov">         21 :       loss_msg_stream &lt;&lt; &quot; (* &quot; &lt;&lt; loss_weight</span>
<span class="lineNum">     412 </span><span class="lineCov">         21 :                       &lt;&lt; &quot; = &quot; &lt;&lt; loss_weight * mean_score &lt;&lt; &quot; loss)&quot;;</span>
<span class="lineNum">     413 </span>            :     }
<span class="lineNum">     414 </span><span class="lineCov">        126 :     LOG(INFO) &lt;&lt; &quot;    Test net output #&quot; &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; output_name &lt;&lt; &quot; = &quot;</span>
<span class="lineNum">     415 </span><span class="lineCov">         42 :               &lt;&lt; mean_score &lt;&lt; loss_msg_stream.str();</span>
<span class="lineNum">     416 </span>            :   }
<span class="lineNum">     417 </span>            : }
<span class="lineNum">     418 </span>            : 
<span class="lineNum">     419 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     420 </span><span class="lineCov">          2 : void Solver&lt;Dtype&gt;::Snapshot() {</span>
<span class="lineNum">     421 </span><span class="lineCov">          4 :   CHECK(Caffe::root_solver());</span>
<span class="lineNum">     422 </span>            :   string model_filename;
<span class="lineNum">     423 </span><span class="lineCov">          2 :   switch (param_.snapshot_format()) {</span>
<span class="lineNum">     424 </span>            :   case caffe::SolverParameter_SnapshotFormat_BINARYPROTO:
<span class="lineNum">     425 </span><span class="lineCov">          4 :     model_filename = SnapshotToBinaryProto();</span>
<span class="lineNum">     426 </span><span class="lineCov">          2 :     break;</span>
<span class="lineNum">     427 </span>            :   case caffe::SolverParameter_SnapshotFormat_HDF5:
<span class="lineNum">     428 </span><span class="lineNoCov">          0 :     model_filename = SnapshotToHDF5();</span>
<span class="lineNum">     429 </span><span class="lineNoCov">          0 :     break;</span>
<span class="lineNum">     430 </span>            :   default:
<span class="lineNum">     431 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unsupported snapshot format.&quot;;</span>
<span class="lineNum">     432 </span>            :   }
<span class="lineNum">     433 </span>            : 
<span class="lineNum">     434 </span><span class="lineCov">          2 :   SnapshotSolverState(model_filename);</span>
<span class="lineNum">     435 </span><span class="lineCov">          2 : }</span>
<span class="lineNum">     436 </span>            : 
<span class="lineNum">     437 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     438 </span><span class="lineCov">          1 : void Solver&lt;Dtype&gt;::CheckSnapshotWritePermissions() {</span>
<span class="lineNum">     439 </span><span class="lineCov">          1 :   if (Caffe::root_solver() &amp;&amp; param_.snapshot()) {</span>
<span class="lineNum">     440 </span><span class="lineCov">          2 :     CHECK(param_.has_snapshot_prefix())</span>
<span class="lineNum">     441 </span>            :         &lt;&lt; &quot;In solver params, snapshot is specified but snapshot_prefix is not&quot;;
<span class="lineNum">     442 </span><span class="lineCov">          2 :     string probe_filename = SnapshotFilename(&quot;.tempfile&quot;);</span>
<span class="lineNum">     443 </span><span class="lineCov">          2 :     std::ofstream probe_ofs(probe_filename.c_str());</span>
<span class="lineNum">     444 </span><span class="lineCov">          1 :     if (probe_ofs.good()) {</span>
<span class="lineNum">     445 </span><span class="lineCov">          1 :       probe_ofs.close();</span>
<span class="lineNum">     446 </span><span class="lineCov">          1 :       std::remove(probe_filename.c_str());</span>
<span class="lineNum">     447 </span>            :     } else {
<span class="lineNum">     448 </span><span class="lineNoCov">          0 :       LOG(FATAL) &lt;&lt; &quot;Cannot write to snapshot prefix '&quot;</span>
<span class="lineNum">     449 </span>            :           &lt;&lt; param_.snapshot_prefix() &lt;&lt; &quot;'.  Make sure &quot;
<span class="lineNum">     450 </span>            :           &lt;&lt; &quot;that the directory exists and is writable.&quot;;
<span class="lineNum">     451 </span>            :     }
<span class="lineNum">     452 </span>            :   }
<span class="lineNum">     453 </span><span class="lineCov">          1 : }</span>
<span class="lineNum">     454 </span>            : 
<span class="lineNum">     455 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     456 </span><span class="lineCov">          5 : string Solver&lt;Dtype&gt;::SnapshotFilename(const string&amp; extension) {</span>
<span class="lineNum">     457 </span>            :   return param_.snapshot_prefix() + &quot;_iter_&quot; + caffe::format_int(iter_)
<span class="lineNum">     458 </span><span class="lineCov">         20 :     + extension;</span>
<span class="lineNum">     459 </span>            : }
<span class="lineNum">     460 </span>            : 
<span class="lineNum">     461 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     462 </span><span class="lineCov">          2 : string Solver&lt;Dtype&gt;::SnapshotToBinaryProto() {</span>
<span class="lineNum">     463 </span><span class="lineCov">          4 :   string model_filename = SnapshotFilename(&quot;.caffemodel&quot;);</span>
<span class="lineNum">     464 </span><span class="lineCov">          4 :   LOG(INFO) &lt;&lt; &quot;Snapshotting to binary proto file &quot; &lt;&lt; model_filename;</span>
<span class="lineNum">     465 </span><span class="lineCov">          4 :   NetParameter net_param;</span>
<span class="lineNum">     466 </span><span class="lineCov">          2 :   net_-&gt;ToProto(&amp;net_param, param_.snapshot_diff());</span>
<span class="lineNum">     467 </span>            :   WriteProtoToBinaryFile(net_param, model_filename);
<span class="lineNum">     468 </span><span class="lineCov">          2 :   return model_filename;</span>
<span class="lineNum">     469 </span>            : }
<span class="lineNum">     470 </span>            : 
<span class="lineNum">     471 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     472 </span><span class="lineNoCov">          0 : string Solver&lt;Dtype&gt;::SnapshotToHDF5() {</span>
<span class="lineNum">     473 </span><span class="lineNoCov">          0 :   string model_filename = SnapshotFilename(&quot;.caffemodel.h5&quot;);</span>
<span class="lineNum">     474 </span><span class="lineNoCov">          0 :   LOG(INFO) &lt;&lt; &quot;Snapshotting to HDF5 file &quot; &lt;&lt; model_filename;</span>
<span class="lineNum">     475 </span><span class="lineNoCov">          0 :   net_-&gt;ToHDF5(model_filename, param_.snapshot_diff());</span>
<span class="lineNum">     476 </span><span class="lineNoCov">          0 :   return model_filename;</span>
<span class="lineNum">     477 </span>            : }
<span class="lineNum">     478 </span>            : 
<span class="lineNum">     479 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     480 </span><span class="lineNoCov">          0 : void Solver&lt;Dtype&gt;::Restore(const char* state_file) {</span>
<span class="lineNum">     481 </span><span class="lineNoCov">          0 :   string state_filename(state_file);</span>
<span class="lineNum">     482 </span><span class="lineNoCov">          0 :   if (state_filename.size() &gt;= 3 &amp;&amp;</span>
<span class="lineNum">     483 </span><span class="lineNoCov">          0 :       state_filename.compare(state_filename.size() - 3, 3, &quot;.h5&quot;) == 0) {</span>
<span class="lineNum">     484 </span><span class="lineNoCov">          0 :     RestoreSolverStateFromHDF5(state_filename);</span>
<span class="lineNum">     485 </span>            :   } else {
<span class="lineNum">     486 </span><span class="lineNoCov">          0 :     RestoreSolverStateFromBinaryProto(state_filename);</span>
<span class="lineNum">     487 </span>            :   }
<span class="lineNum">     488 </span><span class="lineNoCov">          0 : }</span>
<a name="489"><span class="lineNum">     489 </span>            : </a>
<span class="lineNum">     490 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     491 </span><span class="lineCov">      10001 : void Solver&lt;Dtype&gt;::UpdateSmoothedLoss(Dtype loss, int start_iter,</span>
<span class="lineNum">     492 </span>            :     int average_loss) {
<span class="lineNum">     493 </span><span class="lineCov">      10001 :   if (losses_.size() &lt; average_loss) {</span>
<span class="lineNum">     494 </span><span class="lineCov">          1 :     losses_.push_back(loss);</span>
<span class="lineNum">     495 </span><span class="lineCov">          1 :     int size = losses_.size();</span>
<span class="lineNum">     496 </span><span class="lineCov">          1 :     smoothed_loss_ = (smoothed_loss_ * (size - 1) + loss) / size;</span>
<span class="lineNum">     497 </span>            :   } else {
<span class="lineNum">     498 </span><span class="lineCov">      10000 :     int idx = (iter_ - start_iter) % average_loss;</span>
<span class="lineNum">     499 </span><span class="lineCov">      20000 :     smoothed_loss_ += (loss - losses_[idx]) / average_loss;</span>
<span class="lineNum">     500 </span><span class="lineCov">      10000 :     losses_[idx] = loss;</span>
<span class="lineNum">     501 </span>            :   }
<span class="lineNum">     502 </span><span class="lineCov">      10001 : }</span>
<span class="lineNum">     503 </span>            : 
<a name="504"><span class="lineNum">     504 </span>            : INSTANTIATE_CLASS(Solver);</a>
<span class="lineNum">     505 </span>            : 
<span class="lineNum">     506 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
